package cn.neusoft.controller;

import cn.neusoft.pojo.User;
import cn.neusoft.query.UserQuery;
import cn.neusoft.service.IUserService;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;
	@PostMapping      //  请求方式 + 请求路径   post  /user
	public ResultBean add(@RequestBody User user) throws Exception {
		System.out.println("----UserController-----add--------");
		if(user.getUserId()==null ) {
			return  userService.add(user);
		}
		return  userService.update(user);
	}

//	@PutMapping
//	public ResultBean upddate(@RequestBody User user) throws Exception {
//		System.out.println("----UserController-----update--------");
//		return userService.update(user);
//	}
	@DeleteMapping("/{id}")
	public ResultBean delete(@PathVariable("id") Integer id) throws Exception {
		System.out.println("----UserController-----delete--------");
		return userService.delete(id);
	}
	@GetMapping("/{id}")
	public ResultBean getById (@PathVariable("id") Integer id) throws Exception {
		System.out.println("----UserController-----getById--------");
		return userService.getById(id);
	}
	@GetMapping("/all")
	public ResultBean getAll () throws Exception {
		System.out.println("----UserController-----getAll--------");
		return userService.getAll();
	}
	@PostMapping("/page")
	public ResultBean getByPage(@RequestBody UserQuery userQuery) throws  Exception {
		System.out.println("----UserController-----getByPage--------");
		System.out.println(userQuery);
		return  userService.getByPage(userQuery);
	}
	@GetMapping("/batch/{ids}")
	public ResultBean getBatchDel(@PathVariable("ids") String ids) throws Exception {
		//因为在前端的时候，直接变成19，21的形式了，在后端只需要进行转换即可
		System.out.println(ids);
		//将字符串进行分割
		String [] strs=ids.split(",");
		List<Integer> integerList=new ArrayList<>();
		for (String str:strs) {
			integerList.add(Integer.parseInt(str));
		}
		System.out.println(integerList);
		return userService.batchDel(integerList);
	}
}
